有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

实现的基本Java服务类

存在具有以下方法的基本服务接口:

public interface BaseService {

    Dto convertToDto(Entity entity);
    List<Dto> convertToDtoList(List<Entity> entityList);
    Entity convertToEntity(Dto dto);
    List<Entity> convertToEntityList(List<Dto> dtoList);

}

现在,方法ConvertToToListconvertToEntityList应该在基本服务实现中实现,然后由其他服务扩展,这样这些方法只需在基本服务中实现一次。ConvertToListconvertToEntityList始终具有相同的实现,只是它们使用不同的实体和dto类型的各自服务类:

public List<Dto> convertToDtoList(List<Entity> entityList) {
    if (entityList == null)
        return null;

    List<Dto> dtoList = new ArrayList<Dto>();
    Iterator<Entity> it = entityList.iterator();

    while (it.hasNext())
        dtoList.add(this.convertToDto(it.next()));

    return dtoList;
}


public List<Entity> convertToEntityList(List<Dto> dtoList) {
    if (dtoList == null)
        return null;

    List<Entity> entityList = new ArrayList<Entity>();
    Iterator<Dto> it = dtoList.iterator();

    while (it.hasNext())
        entityList.add(this.convertToEntity(it.next()));

    return entityList;
}

如何在基本服务中以从各自的实体和dto类型抽象出来的通用方式实现这些方法,以便在扩展此基本服务的每个服务类中使用它们


共 (2) 个答案

  1. # 1 楼答案

    按如下方式操作:

    public interface BaseService <T> {
    
        Dto convertToDto(Entity entity);
        default public List<Dto> convertToDtoList(List<T> entityList) {
            if (entityList == null)
                return null;
    
            List<Dto> dtoList = new ArrayList<Dto>();
            Iterator<T> it = entityList.iterator();
    
            while (it.hasNext())
                dtoList.add(this.convertToDto(it.next()));
    
            return dtoList;
        }
    
        default public List<Entity> convertToEntityList(List<T> dtoList) {
            if (dtoList == null)
                return null;
    
            List<Entity> entityList = new ArrayList<Entity>();
            Iterator<T> it = dtoList.iterator();
    
            while (it.hasNext())
                entityList.add(this.convertToEntity(it.next()));
    
            return entityList;
        }
        Entity convertToEntity(Dto dto);
    }
    

    查看this了解更多信息

  2. # 2 楼答案

    您可以在接口和模板<>中使用默认实现:

    public interface BaseService<D, E> {
    
        D convertToDto(E entity);
    
        E convertToEntity(D dto);
    
        default List<E> convertToEntityList(List<D> dtoList) {
            return Optional.ofNullable(dtoList).orElse(Collections.emptyList()).stream()
                           .map(this::convertToEntity)
                           .filter(Objects::nonNull)
                           .collect(Collectors.toList());
        }
    
        default List<D> convertToDtoList(List<E> entityList) {
            return Optional.ofNullable(entityList).orElse(Collections.emptyList()).stream()
                           .map(this::convertToDto)
                           .filter(Objects::nonNull)
                           .collect(Collectors.toList());
        }
    
    }
    
    public class DtoEntityBaseService implements BaseService<Dto, Entity> {
    
        @Override
        public Dto convertToDto(Entity entity) {}
    
        @Override
        public Entity convertToEntity(Dto dto) {}
    }
    

    You can look at very useful framework that help to do all these transformation. It calls Mapsruct